package com.nus.fellowmark.modules.assignment.controller;

import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.AssignmentEntity;
import com.nus.fellowmark.entity.GradeCalculationConfigEntity;
import com.nus.fellowmark.modules.assignment.dto.ConfigDTO;
import com.nus.fellowmark.modules.assignment.service.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nus.fellowmark.modules.assignment.service.GradeCalculationConfigService;


/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-09-03 20:18:08
 */
@RestController
@RequestMapping("grade_calculation_config")
public class GradeCalculationConfigController {
    @Autowired
    private GradeCalculationConfigService gradeCalculationConfigService;

    @Autowired
    private AssignmentService assignmentService;

    @RequestMapping("/add_or_update")
    public ServerResponseEntity addOrUpdateConfig(@RequestBody ConfigDTO configDTO) {
        boolean success = gradeCalculationConfigService.addOrUpdateConfig(configDTO);
        return ServerResponseEntity.success(success);
    }

    @RequestMapping("/{assignmentId}")
    public ServerResponseEntity getConfig(@PathVariable Integer assignmentId) {
        GradeCalculationConfigEntity config = gradeCalculationConfigService.getGradeConfig(assignmentId);
        // if no config
        if (config == null) {
            boolean success = gradeCalculationConfigService.addOrUpdateConfig(new ConfigDTO(assignmentId, 0, 0.0, 1, 1, 1));
            if (success) {
                GradeCalculationConfigEntity gradeConfig = gradeCalculationConfigService.getGradeConfig(assignmentId);
                return ServerResponseEntity.success(gradeConfig);
            } else {
                return ServerResponseEntity.showFailMsg("Insert default config fail!");
            }
        }
        return ServerResponseEntity.success(config);
    }
}
